package com.demo.javabean;

public class UserBean {
	
	public boolean valid(String username, String password) {
		boolean isValid = false;
		DBAccess db = new DBAccess();
		if(db.createConn()) {
			String sql = "select * from userinfor where username='"+username+"' and passwords='"+password+"'";
			db.query(sql);
			if(db.next()) {
				isValid = true;
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return isValid;
	}
	
	public void testDB()
	{
		DBAccess db = new DBAccess();
		if(db.createConn()) 
		{
			/*
			 * to test the database system
			 * */
			long sleepTime = 10*1000;
			for (int i = 0; i < 4; i++)
			{
				// start of the system time
				long startTime = System.currentTimeMillis();
				
				// execute the query of updating the database table
				String sql = " update userinfor2" +
						     " set    timestamp = fcd_1day_" + (i+1) + ".timestamp," +
						     "        x         = fcd_1day_" + (i+1) + ".x,"         +
						     "        y         = fcd_1day_" + (i+1) + ".y,"         +
						     "        speed     = fcd_1day_" + (i+1) + ".speed,"     +
						     "        angle     = fcd_1day_" + (i+1) + ".angle"      +
						     " from   fcd_1day_" + (i+1) +
						     " where  userinfor2.carid = fcd_1day_" + (i+1) + ".carid;";
				db.query(sql);
				
				// end of the system time
				long endTime = System.currentTimeMillis();
				
				// execute time for database updating
				long executeTime = endTime - startTime;
				
				if ( executeTime <= sleepTime)
				{
					try
					{
						Thread.currentThread();
						Thread.sleep(sleepTime - executeTime);
					}
					catch(Exception e){}
					System.out.println("fcd_1day_" + (i+1) + " updating executes time: " + executeTime + " ms");
				}
				else
				{
					System.out.println("fcd_1day_" + (i+1) + " updating executes time: " + executeTime + " ms");
				}
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
	}

	// query latitude value according to username in the database
	public String Lat(String username) {
		DBAccess db = new DBAccess();
		String reportLat = null;
		if(db.createConn()) {
			String lat = "select reportlat from userinfor where username='"+username+"'";
			reportLat = db.queryData(lat, "reportlat");
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return reportLat;
	}
	
	// query longitude value according to username in the database
	public String Lon(String username) {
		DBAccess db = new DBAccess();
		String reportLon = null;
		if(db.createConn()) {
			String lon = "select reportlon from userinfor where username='"+username+"'";
			reportLon = db.queryData(lon, "reportlon");
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return reportLon;
	}
	
	public boolean isExist(String username) {
		boolean isExist = false;
		DBAccess db = new DBAccess();
		if(db.createConn()) {
			String sql = "select * from userinfor where username='"+username+"'";
			db.query(sql);
			if(db.next()) {
				isExist = true;
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return isExist;
	}
	
	public void add(String username, String password, String lat, String lon) 
	{
		DBAccess db = new DBAccess();
		if(db.createConn()) 
		{			
			String sql = "insert into userinfor(username,passwords,reportlat,reportlon) values('"+username+"','"+password+"',"+lat+","+lon+")"; 
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
	}
}
